package com.example.manualManagement.controller;

import com.example.manualManagement.entity.User;
import com.example.manualManagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserCtrl {

    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    public String test(){

        return "test access";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(Integer userId) {
        int result = userService.delete(userId);
        if(result >= 1){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(User user) {
        int result = userService.Update(user);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public String insert(User user) {
        int result = userService.insertUser(user);
        if(result >= 1){
            return "添加成功";
        }else{
            return "添加失败";
        }
    }

    @RequestMapping("/ListUser")
    @ResponseBody
    public List<User> ListUser() {
        return userService.ListUser();
    }

    @RequestMapping("/ListByName")
    @ResponseBody
    public List<User> ListUserByName(String userName) {
        return userService.findByName(userName);
    }


    @RequestMapping("/page")
    @ResponseBody
    public List<User> page(Integer page){
        return userService.queryPage(page);
    }

    @RequestMapping(value="/rows")
    @ResponseBody
    public int rows(){
        return (int) userService.getRowCount();
    }
}
